package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.RecommendUserVo;
import com.itheima.service.mongo.RecommendService;
import com.itheimma.service.db.QuestionService;
import com.itheimma.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MakeFriendManager {

    @DubboReference
    private RecommendService recommendService;

    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private QuestionService questionService;

    //推荐最佳
    public ResponseEntity findRecommendUser() {
        Long userId = UserHolder.get().getId();
        RecommendUser recommendUser = recommendService.findRecommendUser(userId);

        UserInfo userInfo = userInfoService.findById(recommendUser.getUserId());

        RecommendUserVo recommendUserVo = new RecommendUserVo();
        recommendUserVo.setUserInfo(userInfo);
        recommendUserVo.setFateValue(recommendUser.getScore().longValue());

        return ResponseEntity.ok(recommendUserVo);
    }

    //推荐好友列表
    public ResponseEntity findRecommendUserList(Integer pageNum ,Integer pageSize){
        Long userId = UserHolder.get().getId();
        PageBeanVo pageBeanVo = recommendService.findRecommendUserByPage(userId, pageNum, pageSize);
        List<RecommendUser> recommendUserList =(List<RecommendUser>) pageBeanVo.getItems();

        ArrayList<RecommendUserVo> recommendUserVos = new ArrayList<>();
        //判断是否非空，封装vo
        if (CollectionUtil.isNotEmpty(recommendUserList)) {
            for (RecommendUser recommendUser : recommendUserList) {
                RecommendUserVo recommendUserVo = new RecommendUserVo();
                UserInfo userInfo = userInfoService.findById(recommendUser.getUserId());
                recommendUserVo.setUserInfo(userInfo);
                recommendUserVo.setFateValue(recommendUser.getScore().longValue());
                recommendUserVos.add(recommendUserVo);
            }
        }

        pageBeanVo.setItems(recommendUserVos);
        return ResponseEntity.ok(pageBeanVo);
    }

    //推荐用户详情
    public ResponseEntity findRecommendUserDetails(Long userId){
        Long toUserId = UserHolder.get().getId();

        RecommendUser recommendUser = null;
        if (userId.equals(toUserId) ){
            recommendUser = new RecommendUser();
            recommendUser.setUserId(toUserId);
            recommendUser.setScore(100D);
        }else {
            recommendUser = recommendService.findRecommendUserDetails(userId, toUserId);
        }
        //查推荐用户的用户Id ，把recommendUser.getId直接换成userId，推荐人Id，这样，即使查的是自己也是userId，不是自己也是UserId，看聊天的时候也是userId
        UserInfo userInfo = userInfoService.findById(userId);
        RecommendUserVo vo = new RecommendUserVo();
        vo.setUserInfo(userInfo);
        try {
            vo.setFateValue(recommendUser.getScore().longValue());
        }catch (Exception e){
            //e.printStackTrace();
        }

        return ResponseEntity.ok(vo);

    }

    //查看陌生人问题
    public ResponseEntity findStrangerQuestion(Long strangerUserId){
        Question question = questionService.findById(strangerUserId);
        if (question == null){
            question = new Question();
            question.setUserId(strangerUserId);
        }
        return ResponseEntity.ok(question.getStrangerQuestion());
    }

    //查询卡片
    public ResponseEntity findCard() {
        Long userId = UserHolder.get().getId();
        PageBeanVo pageBeanVo = recommendService.findRecommendUserByPage(userId, 1, 10);
        List<RecommendUser> recommendUserList =(List<RecommendUser>) pageBeanVo.getItems();

        ArrayList<RecommendUserVo> vos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(recommendUserList)) {
            for (RecommendUser recommendUser : recommendUserList) {
                UserInfo userInfo = userInfoService.findById(recommendUser.getUserId());
                RecommendUserVo recommendUserVo = new RecommendUserVo();
                recommendUserVo.setUserInfo(userInfo);
                recommendUserVo.setFateValue(recommendUser.getScore().longValue());
                vos.add(recommendUserVo);
            }
        }
        return ResponseEntity.ok(vos);

    }


}
